﻿Imports System.IO

Public Class AntiVirus
    Structure FolderParams
        Dim scanSub As Boolean
        Dim path As String
    End Structure

    Dim loaded_db As Boolean = False

    Dim Arrfilename(1), Arrcode(1), ArrtType(1), ArrAttrib(1) As String

    Public Event VirusFound(ByVal path As String, ByVal type As String, ByVal Details As String)
    Public Event CurScanning(ByVal file As FileInfo)
    Public Event Completed()
    Public Event Started()


    Public Sub ScanFolder(ByVal para As FolderParams)
        Dim localdir As New DirectoryInfo(para.path)
        Dim i As Integer = 0
        Try
            For Each folder As DirectoryInfo In localdir.GetDirectories
                If para.scanSub = True Then
                    Dim pr As New FolderParams With {.path = folder.FullName, .scanSub = True}
                    ScanFolder(pr)
                End If
                For i = 0 To UBound(Arrfilename)
                    If folder.Name = Arrfilename(i) Then
                        Select Case ArrtType(i)
                            Case "FOLDER"
                                If folder.Attributes = Integer.Parse(ArrAttrib(i)) Then
                                    RaiseEvent VirusFound(folder.FullName, "FOLDER", Arrcode(i))
                                End If
                                File.Delete(folder.FullName & "\Desktop.ini") 'Remove this file from dir to reveal the autorun virus
                                File.Delete(folder.FullName & "\autorun.exe") 'Main Virus
                            Case "INF"
                                RaiseEvent VirusFound(folder.FullName, "INF", Arrcode(i))
                            Case "EXE"
                                RaiseEvent VirusFound(folder.FullName, "EXE", Arrcode(i))
                        End Select
                    End If
                Next
            Next folder
        Catch ex As UnauthorizedAccessException
            'Just get out from there
        End Try
    End Sub

    Structure FilesParams
        Dim path As String
        Dim FileSpec As String
        Dim scanSub As Boolean
    End Structure

    Public Sub ScanFiles(ByVal para As FilesParams)
        Dim search As New FileSearch(para.path, para.FileSpec, para.scanSub)
        Dim files As List(Of FileInfo) = search.Execute
        RaiseEvent Started()
        For Each file As FileInfo In files
            'RaiseEvent CurScanning(file)
            For i = 0 To UBound(Arrfilename)
                If file.Name = Arrfilename(i) Then
                    Select Case ArrtType(i)
                        Case "INF"
                            If file.Attributes = (ArrAttrib(i)) Then
                                RaiseEvent VirusFound(file.FullName, "INF", Arrcode(i))
                            End If
                        Case "EXE"
                            If file.Attributes = (ArrAttrib(i)) Then
                                RaiseEvent VirusFound(file.FullName, "EXE", Arrcode(i))
                            End If
                    End Select
                End If
            Next
        Next
        RaiseEvent Completed()
    End Sub

    'Loads the virus database file
    Private Sub lDatabase(ByVal db_path As String)
        Dim strname(1) As String
        Dim temp(20) As String
        Dim fileread = IO.File.OpenText(db_path)
        Dim j As Integer, i As Integer = 0
        Do Until fileread.Peek = -1
            If i > UBound(strname) Then
                ReDim Preserve strname(UBound(strname) + 5)
                ReDim Preserve Arrfilename(UBound(Arrfilename) + 5)
                ReDim Preserve Arrcode(UBound(Arrcode) + 5)
                ReDim Preserve ArrtType(UBound(ArrtType) + 5)
                ReDim Preserve ArrAttrib(UBound(ArrAttrib) + 5)
            End If
            strname(i) = fileread.ReadLine
            temp = strname(i).Split(":")
            Arrfilename(i) = temp(0).Trim
            Arrcode(i) = temp(1).Trim
            ArrtType(i) = temp(2).Trim
            ArrAttrib(i) = temp(3).Trim
            i += 1
        Loop
        loaded_db = True
    End Sub

    Public Sub New(ByVal database As String)
        lDatabase(database)
    End Sub

    Public Sub New()
        lDatabase("D:\C.txt")
    End Sub
End Class
